﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.Model;
using rock.UI.common;

namespace rock.UI.fee
{
    public partial class FormFeeShaoRoomZhujing : Form
    {
        DalFEE_ROOM ADalFEE_ROOM = new DalFEE_ROOM();
        private ModelFEE_ROOM aModelFEE_ROOM = new ModelFEE_ROOM();
        public ModelFEE_ROOM _ModelFEE_ROOM 
        {
            get {
                return aModelFEE_ROOM;
            }
            set {
                aModelFEE_ROOM = value;
            }
        }

        public FormFeeShaoRoomZhujing()
        {
            InitializeComponent();

            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void FormFeeShaoRoom_Load(object sender, EventArgs e)
        {

            //年下拉框
            List<ComboBoxInfo> years = new List<ComboBoxInfo>();
            for (int i = 2012; i <= 2042; i++)
            {
                years.Add(new ComboBoxInfo { IntId = i, Name = i + "" });
            }
            this.comboBox1Year.DisplayMember = "Name";
            this.comboBox1Year.ValueMember = "IntId";
            this.comboBox1Year.DataSource = years;

            //月下拉框
            List<ComboBoxInfo> months = new List<ComboBoxInfo>();
            for (int i = 1; i <= 12; i++)
            {
                string value = i < 10 ? ("0" + i) : (i + "");
                months.Add(new ComboBoxInfo { Id = value, Name = value });
            }
            this.comboBox2Month.DisplayMember = "Name";
            this.comboBox2Month.ValueMember = "Id";
            this.comboBox2Month.DataSource = months;


            //更新
            if (this.ADalFEE_ROOM.Exists(this._ModelFEE_ROOM.SEQID))
            {
                this.textBox4Opera.Text = this._ModelFEE_ROOM.OPERA_MAN;
                this.userControlRentContractInfo1._SHOW_BUTTON = false;
                this.userControlRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_ROOM.CONTRACT_SEQID;


                DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                ModelRENT_CONTRACT _ModelRENT_CONTRACT  = _DalRENT_CONTRACT.GetModel(this._ModelFEE_ROOM.CONTRACT_SEQID);
                this.userControlRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID , _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID , _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                this.userControlRentContractInfo1._SHOW_BUTTON = false;

                String monthstr = this._ModelFEE_ROOM.MONTH;
                int year = int.Parse(monthstr.Substring(0, 4));
                string month = monthstr.Substring(4, 2);
                this.comboBox1Year.SelectedValue = year;
                this.comboBox2Month.SelectedValue = month;

        

                this.button3.Enabled = false;
            }
            else
            {   //如果添加又带合同号.则自动加载
                if (this._ModelFEE_ROOM.CONTRACT_SEQID>0)
                {
                    this.userControlRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_ROOM.CONTRACT_SEQID;
                    DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                    ModelRENT_CONTRACT _ModelRENT_CONTRACT  = _DalRENT_CONTRACT.GetModel(this._ModelFEE_ROOM.CONTRACT_SEQID);
                    this.userControlRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID , _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID , _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                }

                //登陆用户
                this.textBox4Opera.Text = Manager.getInstance().userAccount.USERNAME;
                this.userControlRentContractInfo1._SHOW_BUTTON = true;
                this._ModelFEE_ROOM.OPERA_TIME = DateTime.Now;
                this._ModelFEE_ROOM.RECORD_TIME = DateTime.Now;
                this._ModelFEE_ROOM.START_TIME = DateTime.Now;
                this._ModelFEE_ROOM.STOP_TIME = DateTime.Now;


                String monthstr = this._ModelFEE_ROOM.MONTH;
                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;
                this.comboBox1Year.SelectedValue = year;
                string value = month < 10 ? ("0" + month) : (month + "");
                this.comboBox2Month.SelectedValue = value;
             
            }

            //费用状态
 
            this.comboBox1Status.DisplayMember = "Name";
            this.comboBox1Status.ValueMember = "IntId";
            this.comboBox1Status.DataSource = Constant.FEE_ROOM_STATUS_LIST;
            if (this._ModelFEE_ROOM.FEE_STATUS != 0)
            {
                this.comboBox1Status.SelectedValue = this._ModelFEE_ROOM.FEE_STATUS;

            }
            else {
                this.comboBox1Status.SelectedValue = Constant.FEE_ROOM_WEISHAO;
            }

            this.textBox1Note.Text = this._ModelFEE_ROOM.NOTE;
            this.textBox2Money.Text = this._ModelFEE_ROOM.FEE + "";
            this.textBox3BllNumber.Text = this._ModelFEE_ROOM.BILL_NUMBER;
            this.dateTimePicker1Start.Text = this._ModelFEE_ROOM.START_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker2Stop.Text = this._ModelFEE_ROOM.STOP_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker3Opera.Text = this._ModelFEE_ROOM.OPERA_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker2Record.Text = this._ModelFEE_ROOM.RECORD_TIME.ToString(StringUtils.YYYYMMDD);
        }

        private Boolean check()
        {
            try
            {//dalRENT_CUSTOM
                if (this.userControlRentContractInfo1._CONTRACT_SEQID <= 0)
                {
                    throw new Exception("请先选择合同信息");
                }


                StringUtils.checkIsDecimal(this.textBox2Money.Text, "房租金额");

                StringUtils.checkText(this.textBox1Note.Text, 0, 300, "备注信息");
                StringUtils.checkText(this.textBox3BllNumber.Text, 0, 30, "发票号码");

                if (Decimal.Parse(textBox2Money.Text) <= 0)
                {
                    throw new Exception("你还没有输入房租金额.");
                }

                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }

            this._ModelFEE_ROOM.START_TIME = this.dateTimePicker1Start.Value;
            this._ModelFEE_ROOM.STOP_TIME = this.dateTimePicker2Stop.Value;  
                
            this._ModelFEE_ROOM.FEE = Convert.ToDecimal(this.textBox2Money.Text);
            this._ModelFEE_ROOM.NOTE = this.textBox1Note.Text;
            this._ModelFEE_ROOM.OPERA_MAN = this.textBox4Opera.Text;
            this._ModelFEE_ROOM.OPERA_TIME = this.dateTimePicker3Opera.Value;
            this._ModelFEE_ROOM.RECORD_TIME = this.dateTimePicker2Record.Value;
            this._ModelFEE_ROOM.FEE_STATUS = Convert.ToInt32(this.comboBox1Status.SelectedValue);
            this._ModelFEE_ROOM.CONTRACT_SEQID = this.userControlRentContractInfo1._CONTRACT_SEQID;
            this._ModelFEE_ROOM.CUSTOM_SEQID = this.userControlRentContractInfo1._CUSTOM_SEQID;
            this._ModelFEE_ROOM.HOUSE_SEQID = this.userControlRentContractInfo1._ROOM_SEQID;
            this._ModelFEE_ROOM.MONTH = this.comboBox1Year.SelectedValue + "" + this.comboBox2Month.SelectedValue;
            this._ModelFEE_ROOM.BILL_NUMBER = this.textBox3BllNumber.Text;

            if (ADalFEE_ROOM.Exists(this._ModelFEE_ROOM.SEQID))
            {
                DialogResult dialogResult = MessageBox.Show("请确认是否要更新费用情况", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dialogResult.Equals(DialogResult.OK))
                {
                    //刷新数据
                    ADalFEE_ROOM.Update(this._ModelFEE_ROOM);
                }
                else {
                    return;
                }

              
            }
            else
            {
                ADalFEE_ROOM.Add(this._ModelFEE_ROOM);
            }
            //触发代理
            oneRefreshParentData(1);
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //代理
        public delegate void RefreshParentData(int page);
        public RefreshParentData oneRefreshParentData;

        private void button3_Click(object sender, EventArgs e)
        {
             if (this.userControlRentContractInfo1._CONTRACT_SEQID <= 0)
            {

                MessageBox.Show("请先选择合同信息", "提示");
                 return ;  
                      
            }

            //设置合同租金额.
             this.textBox2Money.Text = this.userControlRentContractInfo1._CONTRACT_MONEY + "";


             DataSet ds = this.ADalFEE_ROOM.getLastRecordByContract(this.userControlRentContractInfo1._CONTRACT_SEQID);
             //加载年月为当月
             if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { 
                //没有加载到数据.默认当前.
                 MessageBox.Show("此合同还没有完整的交费记录.默认开始日期为当前.结束日期为开始日期加一个月.", "提示");

                 DateTime lastDate = DateTime.Now;
                 this.dateTimePicker1Start.Value = lastDate;
                 DateTime nextDate = lastDate.AddMonths(1);
                 this.dateTimePicker2Stop.Value = nextDate;


                 return;
                 
             }
            


             //加载上一次最后收租时间

           //  String yyyymm = (String)ds.Tables[0].Rows[0]["YYYYMM"];
             DateTime lastDate2 = (DateTime)ds.Tables[0].Rows[0]["STOP_TIME"];
             this.dateTimePicker1Start.Value = lastDate2; 
             DateTime nextDate2 = (DateTime)ds.Tables[0].Rows[0]["NEXT_STOP"];
             this.dateTimePicker2Stop.Value = nextDate2; 

             //设置到期时间为开始后一个月
        }

        
    }
}
